import json
from models import BaseModels
from sqlalchemy import Column, ForeignKey, DateTime, BigInteger, Integer, JSON, Boolean, Text, String


class Sample(BaseModels):
    __tablename__ = 'sample'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    time = Column(DateTime, index=True, nullable=False, comment="采集时间")
    sequence = Column(String(64), nullable=False, index=True, comment="产品序列SN")
    facility_id = Column(Integer, ForeignKey("facility.id", ondelete='CASCADE'), index=True, comment="经过哪台设备")
    message = Column(JSON, nullable=True, comment="采集信息, JSON 格式数据")
    finish = Column(DateTime, index=True, nullable=False, comment="结束时间")
    option = Column(String(64), nullable=True, comment="操作员名字")
    status_mes = Column(Boolean, nullable=False, comment="MES判断是否合格")
    status_plc = Column(Boolean, nullable=False, comment="PLC判断是否合格的")

    def __repr__(self):
        return json.dumps({
            "sample_id": self.id,
            "time": self.time.strftime("%Y-%m-%d %H:%M:%S"),
            "sequence": self.sequence
        })